package indi.jp.project.process.service.impl;

import indi.jp.project.core.entity.JsonResult;
import indi.jp.project.core.util.SerialUtil;
import indi.jp.project.process.dao.FlowDAO;
import indi.jp.project.process.entity.FlowType;
import indi.jp.project.process.entity.FlowTypeNode;
import indi.jp.project.process.service.FlowService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;

/**
 * 流程实现
 *
 * @author jiangping
 * @date 2022/5/3
 */
@Service
public class FlowServiceImpl implements FlowService {

    @Resource
    private FlowDAO flowDAO;

    @Resource
    private SerialUtil serialUtil;

    @Override
    public JsonResult queryTypeList() throws Exception {
        List<FlowType> list = flowDAO.queryTypeList();
        return JsonResult.success(list);
    }

    @Override
    public JsonResult queryTypeNodes(String typeId) throws Exception {
        LinkedList<FlowTypeNode> list = flowDAO.queryTypeNodeList(typeId);
        return JsonResult.success(list);
    }

    @Override
    public JsonResult queryFlowByTypeCode(String typeCode) throws Exception {
        LinkedList<FlowTypeNode> list = flowDAO.queryFlowByTypeCode(typeCode);
        return JsonResult.success(list);
    }

    @Override
    public JsonResult save(FlowTypeNode flowTypeNode) throws Exception {
        if (StringUtils.isBlank(flowTypeNode.getId())) {
            flowTypeNode.setId(String.valueOf(serialUtil.nextId()));
            flowDAO.insertTypeNode(flowTypeNode);
        } else {
            flowDAO.updateTypeNode(flowTypeNode);
        }
        return JsonResult.success("保存成功");
    }

    @Override
    public JsonResult deleteById(String id) throws Exception {
        int effectRow = flowDAO.deleteById(id);
        if (effectRow > 0) {
            return JsonResult.success("删除成功");
        } else {
            return JsonResult.failure("删除失败，未能找到该数据");
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public JsonResult initTypeNodes(FlowTypeNode flowTypeNode) throws Exception {
        flowDAO.deleteInitTypeNodes(flowTypeNode.getTypeId());

        FlowTypeNode start = new FlowTypeNode();
        start.setId(String.valueOf(serialUtil.nextId()));
        start.setTypeId(flowTypeNode.getTypeId());
        start.setTypeCode(flowTypeNode.getTypeCode());
        start.setTypeName(flowTypeNode.getTypeName());
        start.setName("新建");
        start.setCategory(0);
        start.setState(0);
        start.setStateName("新建");
        start.setRequired(false);

        FlowTypeNode complete = new FlowTypeNode();
        complete.setId(String.valueOf(serialUtil.nextId()));
        complete.setTypeId(flowTypeNode.getTypeId());
        complete.setTypeCode(flowTypeNode.getTypeCode());
        complete.setTypeName(flowTypeNode.getTypeName());
        complete.setName("审批通过");
        complete.setCategory(0);
        complete.setState(99);
        complete.setStateName("审批通过");
        complete.setRequired(false);

        flowDAO.insertTypeNode(start);
        flowDAO.insertTypeNode(complete);
        return JsonResult.success("初始化成功");
    }
}
